package edu.msg.jthr.backend.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Candidate;
import edu.msg.jthr.backend.repository.exception.RepositoryException;
import edu.msg.jthr.backend.repository.service.CandidateRepository;
import edu.msg.jthr.backend.service.CandidateService;

@Stateless(name = "CandidateServiceBean", mappedName = "ejb/CandidateService")
public class CandidateServiceBean implements CandidateService {

	@EJB
	private CandidateRepository candidateRepository;
	private static Logger LOG = LoggerFactory
			.getLogger(CandidateServiceBean.class);

	@Override
	public List<Candidate> getAll() throws ServiceException {
		try {
			return candidateRepository.getAll();
		} catch (RepositoryException e) {
			LOG.error("Candidate getById error! + e.getMessage()");
			throw new ServiceException("Get all Candidates error!", e);
		}
	}

	@Override
	public Candidate getById(Long id) throws ServiceException {
		try {
			return candidateRepository.getById(id);
		} catch (RepositoryException e) {
			LOG.error("Candidate getById error! + e.getMessage()");
			throw new ServiceException("Candidate getById error!", e);
		}
	}

	@Override
	public void create(Candidate obj) throws ServiceException {
		try {
			candidateRepository.create(obj);
		} catch (RepositoryException e) {
			LOG.error("Candidate creating failure! + e.getMessage()");
			throw new ServiceException("Candidate creating failure!", e);
		}
	}

	@Override
	public Candidate update(Candidate obj) throws ServiceException {
		try {
			return candidateRepository.update(obj);
		} catch (RepositoryException e) {
			LOG.error("Candidate update failure! + e.getMessage()");
			throw new ServiceException("Candidate update failure!", e);
		}
	}

	@Override
	public void delete(Candidate obj) throws ServiceException {
		try {
			candidateRepository.delete(obj);
		} catch (RepositoryException e) {
			LOG.error("Candidate delete failure! + e.getMessage()");
			throw new ServiceException("Candidate delete failure!", e);
		}
	}

}
